﻿using Mvc2Ng.Web.NgLib.Extensions.Attributes;
using System;
using System.Linq;
using System.Web.Optimization;

namespace Mvc2Ng.Web.UI {
  public class BundleConfig {
    public static void RegisterBundles(BundleCollection bundles) {

      bundles.IgnoreList.Clear();
      AddDefaultIgnorePatterns(bundles.IgnoreList);

      /********************** Demo Bundle SPA ********************/

      // we create an isolated environment here, because the SPA works totally different
      // SPA is an easy to use, simplified environment, the portal areas are full blown apps

      bundles.Add(new ScriptBundle("~/scripts/vendor")
          .Include("~/Scripts/jquery.js")
          .Include("~/Scripts/jquery/jquery.validate.js")
          .Include("~/Scripts/jquery/jquery.validate.unobtrusive.js")
          .Include("~/scripts/toastr.js")
          .Include("~/scripts/angular.js")
          .Include("~/scripts/angular-animate.js")
          .Include("~/scripts/angular-route.js")
          .Include("~/scripts/angular-sanitize.js")
          .Include("~/scripts/angular-ui-router.js")
          .Include("~/Scripts/q.js")
        // Bootstrap
          .Include("~/scripts/bootstrap.js")
          .Include("~/scripts/ddlevelsmenu.js")
          .Include("~/scripts/jquery.cslider.js")
          .Include("~/scripts/jquery.prettyPhoto.js")
          .Include("~/scripts/jquery.isotope.js")
          .Include("~/scripts/custom.js")
          .Include("~/scripts/filter.js")
          .Include("~/scripts/html5shim.js")
          .Include("~/scripts/jquery.cslider.js")
          .Include("~/scripts/jquery.isotope.js")
          .Include("~/scripts/j.js")
          .Include("~/scripts/moment.js")
          .Include("~/scripts/ui-bootstrap-0.6.0.js")
          .Include("~/scripts/ui-bootstrap-tpls-0.6.0.js")
          .Include("~/scripts/spin.js")
       );

      bundles.Add(new ScriptBundle("~/scripts/app")
        // Bootstrapping -->
          .Include("~/app/app.js")
          .Include("~/app/config.js")
          .Include("~/app/config.exceptionHandler.js")
          .Include("~/app/config.route.js")
        // common Modules -->
          .Include("~/app/common/common.js")
          .Include("~/app/common/logger.js")
          .Include("~/app/common/spinner.js")
        // common.bootstrap Modules -->
          .Include("~/app/common/bootstrap/bootstrap.dialog.js")
        // app -->
          .Include("~/app/layout/shell.js")
          .Include("~/app/layout/topnav.js")
        // app Services -->
        //.Include("~/app/services/datacontext.js") // use api controller GetDataContext()
          .Include("~/app/services/directives.js")
          .Include("~/app/services/currents.js")
        );

      // add controllers automatically
      var controllers = typeof(BundleConfig).Assembly.GetTypes().Where(t => t.GetCustomAttributes(true).OfType<AngularControllersAttribute>().Any());
      var controllerScriptBundle = new ScriptBundle("~/scripts/autocontrollers");
      foreach (var controller in controllers.SelectMany(c => c.GetCustomAttributes(true).OfType<NgControllerAttribute>())) {
        controllerScriptBundle.Include("~/Views/" + controller.ControllerPath);
      }

      bundles.Add(controllerScriptBundle);

      bundles.Add(
        new StyleBundle("~/content/app")
          .Include("~/Styles/bootstrap.css")
          .Include("~/Styles/ddlevelsmenu-base.css")
          .Include("~/Styles/ddlevelsmenu-topbar.css")
          .Include("~/Styles/font-awesome.css")
          .Include("~/Styles/prettyPhoto.css")
          .Include("~/Styles/slider.css")
          .Include("~/Styles/style.css")
          .Include("~/Styles/toastr.css")
        );

    }

    public static void AddDefaultIgnorePatterns(IgnoreList ignoreList) {
      if (ignoreList == null) {
        throw new ArgumentNullException("ignoreList");
      }

      ignoreList.Ignore("*.intellisense.js");
      ignoreList.Ignore("*-vsdoc.js");

      //ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
      //ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
      //ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
    }

  }
}
